package com.complex.client.ui;

import com.complex.client.service.gen.WebServiceMap;

import javax.swing.event.EventListenerList;
import javax.swing.event.TableModelEvent;
import javax.swing.event.TableModelListener;
import javax.swing.table.TableModel;
import java.util.ArrayList;
import java.util.EventListener;
import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * User: AGrankin
 * Date: 28.08.13
 * Time: 13:28
 * To change this template use File | Settings | File Templates.
 */
public class TableAttributesModel implements TableModel {

    private EventListenerList listenerList = new EventListenerList();
    private WebServiceMap attributesList = null;


    public TableAttributesModel(WebServiceMap attrsList) {
        this.attributesList = attrsList;
    }

    public void setData(WebServiceMap attrsList) {
        this.attributesList = attrsList;
        fireTableStructureChanged();
    }

    @Override
    public int getRowCount() {
        if (attributesList != null) {
            return attributesList.getItem().size();
        } else {
            return 0;
        }
    }

    @Override
    public int getColumnCount() {
        return 2;
    }

    @Override
    public String getColumnName(int columnIndex) {
        if (columnIndex == 0) {
            return "Name";
        } else {
            return "Value";
        }
    }

    @Override
    public Class<?> getColumnClass(int columnIndex) {
        return String.class;  //To change body of implemented methods use File | Settings | File Templates.
    }

    @Override
    public boolean isCellEditable(int rowIndex, int columnIndex) {
        return false;  //To change body of implemented methods use File | Settings | File Templates.
    }

    @Override
    public Object getValueAt(int rowIndex, int columnIndex) {
        if (attributesList != null) {
            if (columnIndex == 0) {
                return attributesList.getItem().get(rowIndex).getKey();
            } else {
                return attributesList.getItem().get(rowIndex).getValue();
            }
        } else {
            return null;
        }
    }

    @Override
    public void setValueAt(Object aValue, int rowIndex, int columnIndex) {
        //To change body of implemented methods use File | Settings | File Templates.
    }

    @Override
    public void addTableModelListener(TableModelListener l) {
        listenerList.add(TableModelListener.class, l);
    }

    @Override
    public void removeTableModelListener(TableModelListener l) {
        listenerList.remove(TableModelListener.class, l);
    }

    protected void fireTableStructureChanged() {
        TableModelEvent event = new TableModelEvent(this);
        EventListener[] listeners = listenerList.getListeners(TableModelListener.class);
        for (int i = 0; i<listeners.length; i++) {
            ((TableModelListener)listeners[i]).tableChanged(event);
        }
    }
}
